package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.service.AttributeService;
import cn.tedu.csmall.product.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 程旭东
 */
@Slf4j
@RestController
@RequestMapping("/attr")
public class AttrController {

    @Autowired
    private AttributeService attributeService;

    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody AttributeDTO attributeDTO){
        attributeService.addNewAttr(attributeDTO);
        return JsonResult.ok();
    }

    @GetMapping("/list-by-template")
    public JsonResult listByTemplate(Long templateId){

        log.debug("接收到的templateId为：{}",templateId);
        List<AttributeListItemVO> list = attributeService.listByTemplateId(templateId);
        return JsonResult.ok(list);
    }

    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult deleteById(@PathVariable("id") Long id){
        attributeService.deleteById(id);
        return JsonResult.ok();
    }
}
